#include "ChatClient.h"
string gettime()
{
time_t t;
struct tm *tmp;
char timebuff[MAX_TIMEBUFF];
time(&t);
tmp= localtime(&t);
strftime(timebuff,MAX_TIMEBUFF,"time : %c",tmp);
return timebuff;
}
void getmessage(int connfd)
{
string sbuff="ok";
string amsg;
while(1)
{
//time
if((amsg=datadeal(connfd,sbuff))!="end") //先发送
cout<<amsg<<endl;
else break;
}
}

void chat(int connfd)
{
string input,message,time,username;
while(1)
{
cout<<"select or add friend? or \"back\'"<<endl;
cin>>input;
if(input=="select")
while(1)
{
cout<<datadeal(connfd,input)<<endl; //my friends
cout<<"select who or \"back\""<<endl;;
cin>>input;
getline(cin,message);   //忽略回车符
if(datadeal(connfd,input)=="ok")
{
cout<<"chatting with "<<input<<endl;
while(1)
{
cout<<"from "<<input<<" : "<<endl;
getmessage(connfd);
//发送消息，时间
cout<<endl;
cout<<"me : ";
//cin>>message;
getline(cin,message); 
cout<<message<<endl;
if(message!="back")
{
time=gettime();
cout<<time<<endl;
cout<<endl;
datadeal(connfd,time);
datadeal(connfd,message);}
}
}
else if(input=="back") break;
else cout<<input<<" is not your friend"<<endl;
}
else if(input=="add")
{
if(datadeal(connfd,input)=="ok"); //add friends
cout<<"input user you want to add"<<endl;
cin>>username;
if(datadeal(connfd,username)=="ok")
cout<<"add successful"<<endl;
else cout<<"user don`t exist"<<endl;
}
else if(input=="back")
{
if(datadeal(connfd,input)=="ok");
break;}
else cout<<"command not found"<<endl;
}

}
//接收所有未读消息
//发送消息
/*
}
}
else if(add friend)
while(1)
{
cout<<"add who"<<"back"<<endl;
cin>>name;
//inmap查看服务器数据
if(inmap)
cout<<"success"<<endl;
else cout<<"user don`t exist"<<endl;
if(back) break;
}

else if(back) break;
else cout<<"try again"<<endl;
}
}*/
